package com.work.tqyb.vm;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ToastUtils;
import com.work.tqyb.base.BaseViewModel;
import com.work.tqyb.http.response.AirCityResponse;
import com.work.tqyb.http.response.AirHistoryHoursResponse;
import com.work.tqyb.http.response.AirHistoryResponse;
import com.work.tqyb.http.response.AirLiveResponse;
import com.work.tqyb.http.response.AirProvincesResponse;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AirVM extends BaseViewModel {
    /**
     * 全国城市空气质量key
     */
    private AirCityResponse.CityInfo choiceCity;
    private String key = "e051f26c88e7e85e27450c441162d6f0";
    private MutableLiveData<AirLiveResponse.Result> airLiveLD = new MutableLiveData<>();
    private MutableLiveData<ArrayList<AirProvincesResponse.ProvincesInfo>> airProvincesLD = new MutableLiveData<>();
    private MutableLiveData<ArrayList<AirCityResponse.CityInfo>> airCityLD = new MutableLiveData<>();
    private MutableLiveData<ArrayList<AirHistoryResponse.Result.CityAirInfoBean>> airHistoryLD = new MutableLiveData<>();
    private MutableLiveData<ArrayList<AirHistoryHoursResponse.Result.CityAirInfoBean>> airHistoryHoursLD = new MutableLiveData<>();


    public AirCityResponse.CityInfo getChoiceCity() {
        return choiceCity;
    }

    public void setChoiceCity(AirCityResponse.CityInfo choiceCity) {
        this.choiceCity = choiceCity;
    }

    public LiveData<ArrayList<AirProvincesResponse.ProvincesInfo>> getAirProvincesLD() {
        return airProvincesLD;
    }

    public LiveData<ArrayList<AirCityResponse.CityInfo>> getAirCityLD() {
        return airCityLD;
    }

    public LiveData<AirLiveResponse.Result> getAirLiveLD() {
        return airLiveLD;
    }

    public LiveData<ArrayList<AirHistoryResponse.Result.CityAirInfoBean>> getAirHistoryLD() {
        return airHistoryLD;
    }

    public LiveData<ArrayList<AirHistoryHoursResponse.Result.CityAirInfoBean>> getAirHistoryHoursLD() {
        return airHistoryHoursLD;
    }


    public void airProvinces() {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("key", key);
        apiService
                .airProvinces(stringStringHashMap)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<AirProvincesResponse> call, Response<AirProvincesResponse> response) {
                        if (response.body().result == null) {
                            ToastUtils.showShort(response.body().reason);
                            return;
                        }
                        airProvincesLD.postValue(response.body().result);
                    }

                    @Override
                    public void onFailure(Call<AirProvincesResponse> call, Throwable t) {

                    }
                });
    }

    public void airCitys(String pId) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("key", key);
        stringStringHashMap.put("pId", pId);
        apiService
                .airCitys(stringStringHashMap)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<AirCityResponse> call, Response<AirCityResponse> response) {
                        if (response.body().result == null) {
                            ToastUtils.showShort(response.body().reason);
                            return;
                        }
                        airCityLD.postValue(response.body().result);
                    }

                    @Override
                    public void onFailure(Call<AirCityResponse> call, Throwable t) {

                    }
                });
    }

    public void airLive() {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("key", key);
        stringStringHashMap.put("cityId", choiceCity.Id);
        apiService
                .airLive(stringStringHashMap)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<AirLiveResponse> call, Response<AirLiveResponse> response) {
                        if (response.body().result == null) {
                            ToastUtils.showShort(response.body().reason);
                            return;
                        }
                        airLiveLD.postValue(response.body().result);
                    }

                    @Override
                    public void onFailure(Call<AirLiveResponse> call, Throwable t) {

                    }
                });
    }

    public void airHistory(String cityId) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("key", key);
        stringStringHashMap.put("cityId", cityId);
        apiService
                .airHistory(stringStringHashMap)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<AirHistoryResponse> call, Response<AirHistoryResponse> response) {
                        if (response.body().result == null) {
                            ToastUtils.showShort(response.body().reason);
                            return;
                        }
                        airHistoryLD.postValue(response.body().result.Data);
                    }

                    @Override
                    public void onFailure(Call<AirHistoryResponse> call, Throwable t) {

                    }
                });
    }

    public void airHistoryHours(String cityId) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("key", key);
        stringStringHashMap.put("cityId", cityId);
        apiService
                .airHistoryHours(stringStringHashMap)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<AirHistoryHoursResponse> call, Response<AirHistoryHoursResponse> response1) {
                        if (response1.body().result == null) {
                            ToastUtils.showShort(response1.body().reason);
                            return;
                        }
                        airHistoryHoursLD.postValue(response1.body().result.Data);
                    }

                    @Override
                    public void onFailure(Call<AirHistoryHoursResponse> call, Throwable t) {

                    }
                });
    }
}
